#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    this->resize(999, 666);
    setWindowTitle(tr("UserInfo"));

    // 左侧控件
    userLabel = new QLabel(tr("user :"));
    userEdit = new QLineEdit;
    nameLabel = new QLabel(tr("name :"));
    nameEdit = new QLineEdit;
    sexLabel = new QLabel(tr("sex :"));

    // 下拉列表框
    sexComboBox = new QComboBox;
    sexComboBox->addItem(tr("man"));
    sexComboBox->addItem(tr("woman"));

    departLabel = new QLabel(tr("department :"));
    departEdit = new QTextEdit;
    ageLabel = new QLabel(tr("age :"));
    ageEdit = new QLineEdit;
    // setFrameStyle()设置控件风格
    otherLabel = new QLabel(tr("other :"));
    otherLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    leftLayout = new QGridLayout();
    leftLayout->addWidget(userLabel, 0, 0);
    leftLayout->addWidget(userEdit, 0, 1);
    leftLayout->addWidget(nameLabel, 1, 0);
    leftLayout->addWidget(nameEdit, 1, 1);
    leftLayout->addWidget(sexLabel, 2, 0);
    leftLayout->addWidget(sexComboBox, 2, 1);
    leftLayout->addWidget(departLabel, 3, 0);
    leftLayout->addWidget(departEdit, 3, 1);
    leftLayout->addWidget(ageLabel, 4, 0);
    leftLayout->addWidget(ageEdit, 4, 1);
    leftLayout->addWidget(otherLabel, 5, 0, 1, 2);

    // 设置占用比例。两列的比例为1:3，对话框大小改变后，两列的比例保持不变
    leftLayout->setColumnStretch(0, 1);
    leftLayout->setColumnStretch(1, 3);

    // 右侧上部
    headLabel = new QLabel(tr("head :"));
    // QLabel显示图片，将图片放到构建后生成的Debug目录下
    headIconLabel = new QLabel;
    QPixmap icon("head.jpg");
    headIconLabel->setPixmap(icon);
    // 调整显示图片的大小为图片本身大小
    headIconLabel->resize(icon.width(), icon.height());
    updateButton = new QPushButton(tr("update"));

    // 右侧上部布局
    topRightLayout = new QHBoxLayout();
    topRightLayout->setSpacing(20);
    topRightLayout->addWidget(headLabel);
    topRightLayout->addWidget(headIconLabel);
    topRightLayout->addWidget(updateButton);

    introductionLabel = new QLabel(tr("introduction :"));
    introductionEdit = new QTextEdit;

    // 整个右侧的布局
    rightLayout = new QVBoxLayout();
    rightLayout->setMargin(10);
    rightLayout->addLayout(topRightLayout);
    rightLayout->addWidget(introductionLabel);
    rightLayout->addWidget(introductionEdit);

    // 底部按钮布局
    okButton = new QPushButton(tr("OK"));
    cancelButton = new QPushButton(tr("CANCEL"));
    buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch(); // 在按钮之前插入可升缩控件（占位符），使得两个按钮可以靠右对其
    buttonLayout->addWidget(okButton);
    buttonLayout->addWidget(cancelButton);

    mainLayout = new QGridLayout(this);
    mainLayout->setMargin(15); // 边框距
    mainLayout->setSpacing(10); // 设置控件水平和垂直之间的间隔
    mainLayout->addLayout(leftLayout, 0, 0);
    mainLayout->addLayout(rightLayout, 0, 1);
    mainLayout->addLayout(buttonLayout, 1, 0, 1, 2);
    mainLayout->setSizeConstraint(QLayout::SetFixedSize); // 使得无法改变对话框大小
}

Dialog::~Dialog()
{

}
